/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.sys.entity.Post;
import cn.net.ecode.modules.sys.service.PostService;

/**
 * 岗位管理Controller
 * @author 冯伟、ThinkGem
 * @version 2015-7-29
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/post")
public class PostController extends BaseController {

	@Autowired
	private PostService postService;
	
	@ModelAttribute
	public Post get(String postCode, boolean isNewRecord) {
		return postService.get(postCode, isNewRecord);
	}
	
	@RequiresPermissions("sys:post:view")
	@RequestMapping(value = {"list", ""})
	public String list(Post post, Model model) {
		return "modules/sys/postList";
	}
	
	@RequiresPermissions("sys:post:view")
	@ResponseBody
	@RequestMapping(value = {"listData"})
	public Page<Post> listData(Post post, HttpServletRequest request, HttpServletResponse response) {
		Page<Post> page = postService.findPage(new Page<Post>(request, response), post); 
		return page;
	}

	@RequiresPermissions("sys:post:view")
	@RequestMapping(value = "form")
	public String form(Post post, Model model) {
		model.addAttribute("post", post);
		return "modules/sys/postForm";
	}

	@RequiresPermissions("sys:post:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(Post post, Model model, RedirectAttributes redirectAttributes) {
		if (Global.isDemoMode()) {
			return renderResult(Global.FALSE, "演示模式，不允许操作！");
		}
		StringBuilder message = new StringBuilder();
		if (!beanValidator(message, post)) {
			return renderResult(Global.FALSE, message.toString());
		}
		if (!"true".equals(checkName(post.getOldName(), post.getPostName()))) {
			return renderResult(Global.FALSE, "保存岗位'" + post.getPostName() + "'失败, 岗位名已存在");
		}
		postService.save(post);
		return renderResult(Global.TRUE, "保存岗位'" + post.getPostName() + "'成功！");
	
	}
	
//	@RequiresPermissions("sys:post:edit")
//	@ResponseBody
//	@RequestMapping(value = "delete")
//	public String delete(Post post, RedirectAttributes redirectAttributes) {
//		postService.delete(post);
//		return renderResult(Global.TRUE, "删除岗位成功");
//	}
	
	@RequiresPermissions("sys:post:disable")
	@ResponseBody
	@RequestMapping(value = {"disable"})
	public String disable(Post post, HttpServletRequest request, HttpServletResponse response, Model model) {
		postService.disable(post);
		return renderResult(Global.TRUE, "停用岗位'" + post.getPostName() + "'成功！");
	}
	
	@RequiresPermissions("sys:post:enable")
	@ResponseBody
	@RequestMapping(value = {"enable"})
	public String enable(Post post, HttpServletRequest request, HttpServletResponse response, Model model) {
		postService.enable(post);
		return renderResult(Global.TRUE, "启用岗位'" + post.getPostName() + "'成功！");
	}
	
	/**
	 * 验证岗位名是否有效
	 * @param oldName
	 * @param name
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "checkName")
	public String checkName(String oldName, String postName) {
		if (postName != null && postName.equals(oldName)) {
			return "true";
		} else if (postName != null && postService.getByName(postName) == null) {
			return "true";
		}
		return "false";
	}

}